﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA
{
    public partial class Form_BG : Form
    {
        public int formLanguageIndex { get; set; } = -1;

        public BackgroundWorker bgWorker=new BackgroundWorker();
        bool is_Starting=false;

        Action actGetData;
        Task tskGetData;
        public Form_BG()
        {
            InitializeComponent();
            MultipleLanguage.loadLastFormLanguageSet();//加载最后一次选择的语言种类
            MultipleLanguage.loadFormLanguageSetting();
            bool isRegisted=  HslCommunication.Authorization.SetAuthorizationCode("e3e27344-1a9f-4d8b-b4fc-da5cae4b99bf");
            if (!isRegisted) MessageBox.Show("软件注册失败！");
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        private void Form_BG_Load(object sender , EventArgs e)
        {
            netForms.fmbg = this;
            this.Location = new Point(-8 , -8);
            netForms.bgWidth = Screen.PrimaryScreen.Bounds.Width;
            netForms.bgHeight = Screen.PrimaryScreen.Bounds.Height;
            this.Size = new Size(netForms.bgWidth + 12 , netForms.bgHeight + 12);
            lbl_Wait.Location=new Point((this.Width- lbl_Wait.Width )/ 2,(this.Height -lbl_Wait.Height )/2);
            this.Show();
            if (formLanguageIndex != MultipleLanguage.systemLanguageIndex) MultipleLanguage.switchLanguage(this);
            DBBase.LoadDBSet();
            NetLine.LoadSetting();
            fdPublicFunction.LoadBoxColorset(); 
            lbl_Wait.Visible = false;
            bgWorker.WorkerReportsProgress = true;
            bgWorker.WorkerSupportsCancellation = true;
            bgWorker.DoWork += BgWorker_DoWork;
            netForms.SetForms();
            netForms.fmTop.Show();
            netForms.fmMoniter.Show();
            is_Starting = true;
            bgWorker.RunWorkerAsync();
        }

        private async void BgWorker_DoWork(object sender , DoWorkEventArgs e)
        {
            SetAction();
            while (is_Starting)
            {
                if (tskGetData.IsCompleted)
                {
                    tskGetData.Dispose();
                    SetAction();
                }
                await Task.Delay(5);
                Application.DoEvents();
            }
        }

        private void SetAction()
        {
            actGetData = new Action(NetLine.GetDataFromPLC);
            tskGetData = new Task(actGetData);
            tskGetData.Start();
        }

        private void Form_BG_Activated(object sender, EventArgs e)
        {

        }
    }
}
